import axios from "axios";
import {Message} from "element-ui";
import router from "@/router";

// 创建一个新的axios对象
const request = axios.create({
    baseURL: 'http://127.0.0.1:8080',// 后端接口地址
    timeout: 50000,
})

// request 拦截器
// 可以在发送请求前做处理(如在请求头中添加token对象)
request.interceptors.request.use(
    (config) => {
        const token = localStorage.getItem('Authorization')
        if (token) {
            config.headers['Authorization'] = token;
        } else {
            console.log("Authorization 为null")
        }
        return config
    },
    (error) => Promise.reject(error)
)

// response 拦截器
// 可以在接口响应后统一处理结果
request.interceptors.response.use(
    (response) => response,
    (error) => {
        console.log('response 拦截器报错',error)
        const {status, message} = error
        // const {message} = data
        Message.error('^^^^^^^^^^^',message)
        if (status !== 200) router.push({name: 'Login'})
        return Promise.reject(error)
    }
)

export default request;
